package handler

import (
	"cargo-hub/cache"
	"cargo-hub/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func HomeAdd(c *gin.Context) {
	homeCode := c.PostForm("homeCode")
	homeName := c.PostForm("homeName")
	homeAdr := c.PostForm("homeAdr")
	data := model.Home{
		HomeCode: homeCode,
		HomeName: homeName,
		HomeAdr:  homeAdr,
	}
	err := model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderHomeAdd(c *gin.Context) {
	orderCode := c.PostForm("orderCode")
	orderNum, _ := strconv.Atoi(c.PostForm("orderNum"))
	oderPrice, _ := strconv.Atoi(c.PostForm("oderPrice"))
	oderShop := c.PostForm("oderShop")
	hid, _ := strconv.Atoi(c.PostForm("hid"))
	data := model.OrderHome{
		OrderCode: orderCode,
		OrderNum:  orderNum,
		OderPrice: oderPrice,
		OderShop:  oderShop,
		Hid:       hid,
	}
	err := model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderHomeList(c *gin.Context) {
	/*
			SELECT
		  order_homes.id AS OrderId,
		  order_homes.order_code AS OrderCode,
		  order_homes.order_num AS OrderNum,
		  order_homes.oder_price AS OrderPrice,
		  order_homes.oder_shop AS OrderShop,
		  homes.home_code AS HomeCode,
		  homes.home_name AS HomeName,
		 homes.home_adr AS HomeAdr
		FROM
		  order_homes
		  JOIN homes ON order_homes.hid = homes.id

	*/
	var orderHomeList []model.OrderHomeList
	redis_key := "list_order"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("order_homes").
			Select("order_homes.id AS OrderId",
				"order_homes.order_code AS OrderCode",
				"order_homes.order_num AS OrderNum",
				"order_homes.oder_price AS OrderPrice",
				"order_homes.oder_shop AS OrderShop",
				"homes.home_code AS HomeCode",
				"homes.home_name AS HomeName",
				"homes.home_adr AS HomeAdr").
			Joins("JOIN homes ON order_homes.hid = homes.id").
			Debug().Find(&orderHomeList)
		jsonData, _ := json.Marshal(orderHomeList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderHomeList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": orderHomeList,
	})
	return
}

func OrderHomeDetail(c *gin.Context) {
	id := c.Query("id")
	var orderHomeList model.OrderHomeList
	redis_key := "detail_order" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("order_homes").
			Select("order_homes.id AS OrderId",
				"order_homes.order_code AS OrderCode",
				"order_homes.order_num AS OrderNum",
				"order_homes.oder_price AS OrderPrice",
				"order_homes.oder_shop AS OrderShop",
				"homes.home_code AS HomeCode",
				"homes.home_name AS HomeName",
				"homes.home_adr AS HomeAdr").
			Joins("JOIN homes ON order_homes.hid = homes.id").
			Where("order_homes.id = ?", id).
			Debug().Find(&orderHomeList)
		jsonData, _ := json.Marshal(orderHomeList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderHomeList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": orderHomeList,
	})
	return
}
